package com.cx.bank.handler;

import com.cx.bank.exception.AccountOverDrawnException;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.SQLException;

/**
 * @author jxau_scf
 * @date 2021/7/25 - 19:22
 */
public class MyInVocationHandlerTest implements InvocationHandler {
    private Object target;

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        find_a_time(args[0].toString());
        find_a_place();
        Object result = method.invoke(this.target, args);
        return result;
    }
    public void find_a_time(String name) {
        System.out.println("某年某月"+name);
    }
    public void find_a_place() {
        System.out.println("某地");
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

//    private void LoggingBankOperation(String user, String methodName, Date date) throws IOException {
//        File logfile = new File("E:\\Worksapces\\idea2019.3.1\\bank1.9\\userLog.txt");
//        if (!logfile.exists()) {
//            FileOutputStream fos = new FileOutputStream(logfile, true);
//            OutputStreamWriter opw = new OutputStreamWriter(fos, "utf-8");
//            opw.append("用户"+user+"|在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"|执行了"+methodName+"操作"+'\n');
//            opw.close();
//            fos.close();
//        }



    public static void main(String[] args) throws AccountOverDrawnException, SQLException {
        Travel person = new Person();
        MyInVocationHandlerTest handlerTest = new MyInVocationHandlerTest();
        handlerTest.setTarget(person);
        Travel proxy = (Travel)handlerTest.getProxy();
//        proxy.toTravel();
        proxy.toEat("鸡腿");


//        IManager manager = ManagerImpl.getInstance();
//        MyInVocationHandlerTest handler = new MyInVocationHandlerTest();
//        handler.setTarget(manager);
//        IManager proxy = (IManager)handler.getProxy();
//        boolean depositStatus = proxy.deposit("aaa", 1);
//        System.out.println(depositStatus);
    }
}
